package client;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Client {
    public enum State {
        LOGIN, CONNECTED, LOCKED
    }
	
    public State state = State.LOGIN;
	
	public JFrame frame;
	public LoginPanel loginPanel;
	public ConnectedPanel connectedPanel;
	public LockPanel lockPanel;
	public BlackPanel blackPanel;
	public MenuPanel menuPanel;
	public ImagePanel imagePanel;
	
	public int width;
	public int height;
	
	public Client(String name){
	    // Create elements (image has to be first, it sets the dimensions of client...
	    imagePanel = new ImagePanel(this);
		frame = new JFrame(name);
	    loginPanel = new LoginPanel(this);
	    connectedPanel = new ConnectedPanel(this);
	    lockPanel = new LockPanel(this);
	    blackPanel = new BlackPanel(this);
	    menuPanel = new MenuPanel(this);
	    
	    // Setup frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(imagePanel);
		
		// Setup frame's panel
	    this.getRootPanel().setLayout(null);
	    this.getRootPanel().add(loginPanel);
		
		this.placeLoginPanel();
		
		frame.pack();
		frame.setVisible(true);
		
	}
	
	public JPanel getRootPanel(){
		return (JPanel)frame.getContentPane();
	}
	
	public void placeLoginPanel(){
		loginPanel.setBounds(width/2-100, height/2-50, 200, 100);
	}
	
	public void placeLockPanel(){
		lockPanel.setBounds(width/2-100, height/2-50, 200, 100);
	}
	
	public  void placeConnectedPanel(){
		connectedPanel.setBounds(0, height-30, 93, 28);
	}
	
	public void placeBlackPanel(){
		blackPanel.setBounds(0, 0, width, height);
	}
	
   public void login() {
        // Switch login panel to connected panel
        this.getRootPanel().remove(this.loginPanel);
        this.getRootPanel().add(this.connectedPanel);
        
        // Place the connected panel
        this.placeConnectedPanel();
        
        // Erase password
        this.loginPanel.clearPass();
        
        // Set state
        this.state = Client.State.CONNECTED;
        
        // Repaint the parent panel
        this.getRootPanel().repaint();
    }
   
   public void cancelLogout() {
       this.getRootPanel().remove(this.menuPanel);
       this.getRootPanel().add(this.connectedPanel);
       this.placeConnectedPanel();
       this.getRootPanel().repaint();
   }
   
   public void logout() {
       this.getRootPanel().remove(this.menuPanel);
       this.getRootPanel().add(this.blackPanel);
       this.placeBlackPanel();
       this.getRootPanel().repaint();
   }
   
   public void lock() {
       this.getRootPanel().remove(this.menuPanel);
       this.getRootPanel().add(this.loginPanel);
       this.state = Client.State.LOCKED;
       this.placeLoginPanel();
       this.getRootPanel().repaint();
   }
   
   public void unlock() {
       this.getRootPanel().remove(this.lockPanel);
       this.getRootPanel().add(this.connectedPanel);
       this.state = Client.State.CONNECTED;
       this.placeConnectedPanel();
       this.getRootPanel().repaint();
   }
   
   public void showMenu() {
       menuPanel.setBounds(0, 0, this.width, this.height);
       this.getRootPanel().remove(this.connectedPanel);
       this.getRootPanel().add(menuPanel);
       this.getRootPanel().repaint();
   }
   
   public void hideMenu() {
       this.getRootPanel().remove(this.menuPanel);
       this.getRootPanel().add(this.connectedPanel);
       this.placeConnectedPanel();
       this.getRootPanel().repaint();
   }
   
   public void removeBlackShowLogin() {
       this.getRootPanel().remove(this.blackPanel);
       this.getRootPanel().add(this.loginPanel);
       this.state = Client.State.LOGIN;
       this.loginPanel.clearBoth();
       this.placeLoginPanel();
       this.getRootPanel().repaint();
   }
	   
	public static void main(String [] args){
		new Client("Client Application");
	}
}
